import sys

def output(word, llinks, lsharp, rsharp):
    print '%s\tx' % word
    if lsharp:
        print '%s\t<' % word
    if rsharp:
        print '%s\t>' % word
    for w in llinks:
        print '%s\tl:%s' % (word, w)
        print '%s\tr:%s' % (w, word)

def main():
    '''
    NOTE: no mapper for this job
    input: links_second.txt
    output: links_third.txt
    (word, {'x', '<', '>', 'l:'linkee..}) -> 
        (word, '<'?)
        (word, '>'?)
        (word, 'l:'linkee)..
        (linkee, 'r:'word)..
    all above will be filtered out if 'x' is not found
    '''
    prev_word = ''
    llinks = []
    lsharp = False
    rsharp = False
    ok = False
    for line in sys.stdin:
        line = line.strip()
        word, rest = line.split()
        if word != prev_word:
            if ok:
                output(prev_word, llinks, lsharp, rsharp)
            llinks = []
            lsharp = False
            rsharp = False
            ok = False
            prev_word = word
        if rest == 'x':
            ok = True
        elif rest == '<':
            lsharp = True
        elif rest == '>':
            rsharp = True
        elif rest.startswith('l:'):
            llinks.append(rest[2: ])
        else:
            raise RuntimeError('invalid key-value pair: %s, %s' % (word, rest))
    if ok:
        output(prev_word, llinks, lsharp, rsharp)

if __name__ == '__main__':
    main()

